﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADDemo2025
{
    public static class CmCurveMethod
    {
        /// <summary>
        /// 求两个曲线的交点,OnBothOperands=不延长、ExtendThis=延长曲线1、ExtendArgument=延长曲线2、ExtendBoth=延长两个曲线
        /// </summary>
        /// <param name="curve1">曲线1</param>
        /// <param name="curve2">曲线2</param>
        /// <param name="num">0=不延长、1=延长曲线1、2=延长曲线2、3=延长两个曲线</param>
        /// <returns></returns>
        public static List<Point3d> IntersectWith(this Curve curve1, Curve curve2, Intersect intersect = Intersect.OnBothOperands)
        {
            Point3dCollection pos = new Point3dCollection();
            curve1.IntersectWith(curve2, intersect, pos, IntPtr.Zero, IntPtr.Zero);
            List<Point3d> points = new List<Point3d>();
            foreach (Point3d item in pos)
            {
                points.Add(item);
            }
            return points;
        }

        /// <summary>
        /// 根据点切割curve
        /// </summary>
        /// <param name="curve"></param>
        /// <param name="points"></param>
        /// <returns></returns>
        public static List<Curve> GetSplitCurves(this Curve curve, List<Point3d> points)
        {
            List<Curve> curves = new List<Curve>();
            List<Point3d> tmps = new List<Point3d>();
            points.ForEach(p => tmps.Add(curve.GetClosestPointTo(p, false)));
            points = tmps.OrderBy(p => curve.GetParameterAtPoint(p)).ToList();
            Point3dCollection pos = new Point3dCollection();
            points.ForEach(p => pos.Add(curve.GetClosestPointTo(p, false)));
            DBObjectCollection dbs = curve.GetSplitCurves(pos);
            foreach (Curve item in dbs)
            {
                curves.Add(item);
            }
            return curves;
        }

        /// <summary>
        /// 两个曲线是否首尾相连
        /// </summary>
        /// <param name="curve1"></param>
        /// <param name="curve2"></param>
        /// <param name="tolerance">容差</param>
        /// <returns></returns>
        public static bool IsConnected(this Curve curve1, Curve curve2, double tolerance)
        {
            Tolerance tol = new Tolerance(tolerance, tolerance);
            Point3d p1 = curve1.StartPoint;
            Point3d p2 = curve1.EndPoint;
            Point3d p3 = curve2.StartPoint;
            Point3d p4 = curve2.EndPoint;
            if (p1.IsEqualTo(p3, tol) || p1.IsEqualTo(p4, tol) || p2.IsEqualTo(p3, tol) || p2.IsEqualTo(p4, tol))
            {
                return true;
            }
            return false;
        }
        public static bool IsConnected(this Curve curve1, Curve curve2, double tolerance,out Line line)
        {
            line = new Line();
            Tolerance tol = new Tolerance(tolerance, tolerance);
            Point3d p1 = curve1.StartPoint;
            Point3d p2 = curve1.EndPoint;
            Point3d p3 = curve2.StartPoint;
            Point3d p4 = curve2.EndPoint;
            if (p1.IsEqualTo(p3, tol))
            {
                line = new Line(p1, p3);
                return true;
            }
            if (p1.IsEqualTo(p4, tol) )
            {
                line = new Line(p1, p4);
                return true;
            }
            if (p2.IsEqualTo(p3, tol))
            {
                line = new Line(p2, p3);
                return true;
            }
            if (p2.IsEqualTo(p4, tol))
            {
                line = new Line(p2, p4);
                return true;
            }
            return false;
        }
    }
}
